<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 这里写表格 -->
      <el-table :data="list" stripe>
        <!-- 每一列 -->
        <el-table-column prop="stem" label="标题" width="350px"></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"></el-table-column>
        <el-table-column prop="views" label="浏览数"></el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="180px"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('view', row.id)"></i>
              <i class="el-icon-edit-outline" @click="openDrawer('edit', row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="current"
        :page-size="pageSize"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-drawer :title="changeTitle" :visible.sync="drawer" :before-close="handleClose" size="50%">
      <!-- 如果是view显示div，如果是其他显示form表单 -->
      <div v-if="drawerType === 'view'">
        <h4>{{ form.stem }}</h4>
        <!-- v-if可以解析标签 -->
        <div v-html="form.content"></div>
      </div>

      <el-form v-else label-width="80px" :model="form" :rules="rules" ref="form">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quillEditor> </el-form-item
        ><el-form-item>
          <el-button type="primary" @click="addOn">确定</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { getList, addAPI, deleteAPI, getDetailAPI, updateAPI } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      total: 0, // 一共有多少数据
      current: 1, // 当前页数
      pageSize: 10, // 每页多少条数据,
      drawer: false, // 控制抽屉显示隐藏
      drawerType: '', // 控制 changeTitle传的参数
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        content: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 获取数据
    async newList () {
      const res = await getList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange (val) {
      // console.log(val)
      this.current = val
      this.newList()
    },
    // 抽屉关闭前做什么
    handleClose () {
      this.drawer = false
      // 重置表单  预览时没有表单，会报错，需要判断一下
      // if (this.$refs.form) {
      //   this.$refs.form.resetFields()
      // }
      this.$refs.form?.resetFields() // 和上面的if判断效果一样，是es6的新语法
    },
    // 打开抽屉
    async openDrawer (type, id) {
      this.drawer = true
      // console.log(type)
      // 将方法传过来的值赋值给drawerType（add。edit。view）
      this.drawerType = type
      // console.log(id)

      if (type !== 'add') {
        const res = await getDetailAPI(id)
        // console.log(res)
        this.form = { ...res.data }
      }
    },
    addOn () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 判断是添加还是修改
          if (this.drawerType === 'add') {
            const { stem, content } = this.form
            // 发送添加数据请求
            await addAPI({ stem, content })
            // 提示
            this.$message.success('添加成功')
          } else {
            const { id, stem, content } = this.form
            // 发送修改请求
            await updateAPI({ id, stem, content })
            this.$message.success('修改成功')
          }

          // 关闭抽屉，重置表单
          this.handleClose()

          // 将页码改为1，获取第一页数据
          this.current = 1
          // 重新获取数据
          this.newList()
        }
      })
    },
    del (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送删除请求
          await deleteAPI(id)
          // 提示
          this.$message.success('删除成功')
          // 重新获取数据
          this.newList()
        })
        .catch()
    }
  },
  computed: {
    // 通过计算，修改标题
    changeTitle () {
      // 声明一个空变量
      let title = ''
      // 给变量重新赋值
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'view') title = '预览'
      if (this.drawerType === 'edit') title = '修改面经'
      // 返回这个变量
      return title
    }
  },
  created () {
    this.newList()
  },
  components: {
    quillEditor
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
